package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// UpgradePackage 升级包管理
type UpgradePackage struct {
	ID           string `json:"id" gorm:"column:id;primaryKey;type:bigint;size:64;comment:主键"`
	Version      string `json:"version" gorm:"column:version;type:string;size:40;index:idx_pkg,unique,priority:1;comment:版本号"`
	TerminalType string `json:"terminal_type" gorm:"column:terminal_type;type:string;size:40;index:idx_pkg,unique,priority:2;comment:客户端类型"`
	Arch         int    `json:"arch" gorm:"column:arch;type:int;size:8;index:idx_pkg,unique,priority:3;comment:cpu架构"`
	FilePath     string `json:"file_path" gorm:"column:file_path;type:string;size:255;comment:安装包文件的路径"`
	FileMD5      string `json:"file_md5" gorm:"column:file_md5;type:string;size:32;comment:安装包文件的md5值"`
	ChangeLog    string `json:"change_log" gorm:"column:change_log;type:text;comment:更新日志"`
	CreatedAt    int64  `json:"created_at" gorm:"column:created_at;type:int;size:64;comment:创建时间"`
}

// TableName 表名
func (UpgradePackage) TableName() string {
	return "upgrade_package"
}

// BeforeCreate 自增id
func (u *UpgradePackage) BeforeCreate(tx *gorm.DB) (err error) {
	if u.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	u.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &UpgradePackage{})
}
